<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="编号" prop="no" />

      <el-table-column align="center" label="模板名称" prop="title" />

      <el-table-column align="center" label="模板内容" prop="content" />

      <el-table-column align="center" label="创建时间" prop="createTime" />

      <el-table-column align="center" label="是否启用" prop="enable">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.enable == 0 ? 'success' : 'error' "
          >{{ scope.row.enable == 0 ? '启用' : '不启用' }}</el-tag>
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="广告标题" prop="title">
          <el-input v-model="dataForm.title" />
        </el-form-item>
        <el-form-item label="广告内容" prop="content">
          <el-input v-model="dataForm.content" />
        </el-form-item>
        <el-form-item label="是否启用" prop="enable">
          <el-select v-model="dataForm.enable" placeholder="请选择">
            <el-option :value="0" label="启用" />
            <el-option :value="1" label="不启用" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button  type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style scoped>
.tit {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
<script>
import { listMessage,updatemessage } from "@/api/comment";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination

export default {
  name: "Comment",
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 5,
        type: 1,
        sort: "add_time",
        order: "desc",
      },
      dataForm: {
        id: undefined,
        name: undefined,
        content: undefined,
        url: undefined,
        link: undefined,
        enabled: true
      },
      dialogStatus: '',
      textMap: {
        update: "编辑",
        create: "创建",
      },
      dialogFormVisible: false,
      rules: {
        name: [
          { required: true, message: '广告标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '广告内容不能为空', trigger: 'blur' }
        ],
        url: [{ required: true, message: '广告链接不能为空', trigger: 'blur' }]
      },
      downloadLoading: false,
      replyForm: {
        commentId: 0,
        content: "",
      },
      replyFormVisible: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    
     handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
     handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          updatemessage(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v);
                  this.list.splice(index, 1, this.dataForm);
                  break;
                }
              }
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "更新成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response.data.errmsg,
              });
            });
        }
      });
    },
    getList() {
      this.listLoading = true;
      listMessage(this.listQuery)
        .then((response) => {
          this.list = response.data.data.list;
          this.total = response.data.data.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.list = [];
          this.total = 0;
          this.listLoading = false;
        });
    },
    reply() {
      replyComment(this.replyForm)
        .then((response) => {
          this.replyFormVisible = false;
          this.$notify.success({
            title: "成功",
            message: "核销成功",
          });
        })
        .catch((response) => {
          this.$notify.error({
            title: "失败",
            message: response.data.errmsg,
          });
        });
    },
  },
};
</script>
